<?php
	/**
	 * 获取战绩详情列表
	 */
    class CombatGainsdetail extends APIBase {
		
		public $tag = "CombatGainsdetail";
    	public $isLogin = false;
		
		public function before() {
			$this->initCacheRedis();
			$this->initMysql();
			return true;
		}
		public function logic(){
			//$ann = $this->cache_redis->hMGet("ha:*", array("id","title","content","image_url"));
			//$combatgainsdetail= $this->cache_redis->keys("hCombatGainsdetail:*");
			$deskno=  $this->param['deskno'];
			$date = $this->param['buildDate'];
			$playerID = $this->param['playerID'];
			$roomid = $this->param['roomid'];
			$UserCombatgainsdetail = $this->cache_redis->keys("hUserCombatgainsdetail:$playerID:$date:*");
			if(!empty($UserCombatgainsdetail)){
				$count = count($UserCombatgainsdetail);
				$result = array();
			    if ($count) {
				    for($i=0;$i<$count;$i++){ 
					 $result[] = $this->cache_redis->hGetAll("hUserCombatgainsdetail:$playerID:$date:".$i);   
				    }
	            }
				$this->returnData($result);
			}else{	
			    $sql = "select playerID,score,deskno,gamelog,gameDate,replayCode from customgamelog where deskno=$deskno and deskBuildDate=$date and playerID=$playerID and roomid=$roomid";
                $result = $this->mysql->query($sql);
				$list = array();
				$rows = array();
				if ($result) {
		            while ( $row = mysql_fetch_assoc($result) ) {
			            $rows[] = $row;
		            }
					if($rows){
						for ($i = 0; $i < count($rows); $i++) {
		                 $r = array();
						 $u = array();
		                 $r['playerID'] = (int)$rows[$i]['playerID'];
						 $gamelog = json_decode((string)$rows[$i]['gamelog'],true);
						 $keys = array_keys($gamelog);
                         for($j=0;$j<count($keys);$j++){
							if(is_numeric($keys[$j])){
							$playid = $keys[$j]; 
							$user = $this->mysql->query("select name,avater_url from player where id = $playid limit 1");
						    $userinfo = mysql_fetch_assoc($user);
							$u[$keys[$j]] = array('name'=>$userinfo['name'],'avater_url'=>$userinfo['avater_url']);
							}
						 }
                         $r['userinfo'] = (string)json_encode($u);
		                 $r['score'] = (int)$rows[$i]['score'];
		                 $r['deskno'] = (int)$rows[$i]['deskno'];
		                 $r['gamelog'] = (string)$rows[$i]['gamelog'];
		                 $r['gameDate'] = (int)$rows[$i]['gameDate'];
                         $r['replayCode'] = (string)$rows[$i]['replayCode'];
		                 $r['num'] = $i+1;
		                 $this->cache_redis->hMset("hUserCombatgainsdetail:$playerID:$date:{$i}",$r);
					     $list[] = $r;
	                    }
				        $this->returnData($list);
					}else{
					    $this->returnError(301,"对不起，暂无战绩详情！");
				    }
			    } 
			}
		}
		
        public function after() {
			$this->deinitCacheRedis();
		}
    }
?>
